package com.vooda.business_admin.service.impl;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vooda.business_admin.common.ReqDataTableDto;
import com.vooda.business_admin.common.RspDataTableDto;
import com.vooda.business_admin.dao.SysUserMapper;
import com.vooda.business_admin.entity.SysUser;
import com.vooda.business_admin.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class SysUserServiceImpl implements SysUserService{

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser findByUserName(String userName) {
        return sysUserMapper.findByUserName(userName);
    }

    @Override
    @Transactional(readOnly = false)
    public int deleteByPrimaryKey(Long id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser.setIsDeleted(true);
        return sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }

    @Override
    @Transactional(readOnly = false)
    public int insertSelective(SysUser record) {
        Long maxId = sysUserMapper.findMaxId();
        record.setId(maxId+1);
        record.setPassword(new Md5PasswordEncoder().encodePassword(record.getPassword(), record.getAccount()));
        record.setCreateTime(new Date());
        record.setModifiedTime(new Date());
        record.setIsDeleted(false);
        System.out.println(1);
        return sysUserMapper.insertSelective(record);
    }

    @Override
    public SysUser selectByPrimaryKey(Long id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false)
    public int updateByPrimaryKeySelective(SysUser record) {
        record.setModifiedTime(new Date());
        return sysUserMapper.updateByPrimaryKeySelective(record);
    }


    @Override
    public RspDataTableDto<SysUser> findManagerList(ReqDataTableDto reqDataTableDto) {
        PageHelper.startPage(reqDataTableDto.getPage()+1,reqDataTableDto.getLength());
        List<SysUser> list = sysUserMapper.findAllList();
        PageInfo<SysUser> pageInfo =  new PageInfo<SysUser>(list);
        return new RspDataTableDto<SysUser>(reqDataTableDto.getDraw(),pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    @Transactional(readOnly = false)
    public void saveManager(SysUser sysUser) {
        int count = sysUser.getId() != null?updateByPrimaryKeySelective(sysUser):insertSelective(sysUser);
    }

    @Override
    @Transactional(readOnly = false)
    public void resetPassword(Long id) {
        SysUser sysUser = selectByPrimaryKey(id);
        //重置密码后，密码默认为123456；

        sysUser.setPassword(new Md5PasswordEncoder().encodePassword("123456", sysUser.getAccount()));
        updateByPrimaryKeySelective(sysUser);

    }

    @Override
    public Boolean checkPassword(Long id,String oldPwd) {
        SysUser sysUser = selectByPrimaryKey(id);
        String oldPassword = new Md5PasswordEncoder().encodePassword(oldPwd, sysUser.getAccount());
        return sysUser.getPassword().equals(oldPassword)?true:false;
    }

    @Override
    @Transactional(readOnly = false)
    public void updatePassword(Long id, String newPassword) {
        SysUser sysUser = selectByPrimaryKey(id);
        newPassword = new Md5PasswordEncoder().encodePassword(newPassword, sysUser.getAccount());
        sysUser.setPassword(newPassword);
        updateByPrimaryKeySelective(sysUser);
    }
}
